package com.goldze.mvvmhabit.ui.login;

import android.app.Application;
import android.os.CountDownTimer;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.goldze.mvvmhabit.BR;
import com.goldze.mvvmhabit.data.DemoRepository;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;


public class CodeModel extends BaseViewModel<DemoRepository> {
    public ObservableField<String> userphone = new ObservableField<>("验证码已发送至15745544874");
    public ObservableField<String> logingetcode = new ObservableField<>("");
    public ObservableField<String> logintime = new ObservableField<>("");
    //new倒计时对象,总共的时间,每隔多少秒更新一次时间
    final TimeCount mTiemTimeCount = new TimeCount(60000, 1000);
    //    控件是否可以点击
    public ObservableField<Boolean> ckettrue = new ObservableField<>(true);
    public CodeModel(@NonNull Application application, DemoRepository model) {
        super(application, model);
        userphone.set("验证码已发送至15745544874");
        ckettrue.set(true);
    }

    //登录按钮的点击事件
    public BindingCommand loginconde = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            getcode();
        }
    });

    private void getcode() {
        mTiemTimeCount.start();
    }
    // 计时重发
    private class TimeCount extends CountDownTimer {

        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {

            ckettrue.set(false);
            logintime.set(millisUntilFinished / 1000 + "秒后重新发送");
        }

        @Override
        public void onFinish() {
            ckettrue.set(true);
            logintime.set("重新发送");
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        mTiemTimeCount.cancel();
    }
}
